package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.hjq.widget.layout.SettingBar;
import com.hjq.widget.view.SwitchButton;
import com.qiantu.api.entity.LinkageBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.bean.LocationData;
import com.qiantu.phone.bean.SunriseSunsetTimeBean;
import com.qiantu.phone.ui.activity.map.LocationActivity;
import com.qiantu.phone.ui.dialog.BottomSunriseSunsetDialog;
import com.qiantu.phone.utils.LinkageDataHolder;
import com.lxj.xpopup.XPopup;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * time   : 2021/12/22
 */
public final class SunriseSunsetActivity extends AppActivity {

    private SettingBar settingReferenceArea;
    private SwitchButton btnSwitch;
    private View selectDayLayout;
    private View btnMonday;
    private View btnTuesday;
    private View btnWednesday;
    private View btnThursday;
    private View btnFriday;
    private View btnSaturday;
    private View btnSunday;
    private TextView tvSunriseSunsetCondition;
    private TextView sunriseCheckbox;
    private TextView sunsetCheckbox;
    private BottomSunriseSunsetDialog mBottomSunriseSunsetDialog;
    private int mClickSunriseSunsetType;
    private String serialNo;
    private String areaName;
    private String areaCoding;
    private double latitude;
    private double longitude;
    private int scopeLong;
    private int sunPositionType;
    private int timeLong;
    private LinkageBean mLinkageBean;
    private LinkageBean.LinkageCondition mLinkageCondition;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sunrise_sunset;
    }

    @Override
    protected void initView() {
        settingReferenceArea = findViewById(R.id.setting_reference_area);
        btnSwitch = findViewById(R.id.btn_switch);
        selectDayLayout = findViewById(R.id.select_day_layout);
        btnMonday = findViewById(R.id.btn_monday);
        btnTuesday = findViewById(R.id.btn_tuesday);
        btnWednesday = findViewById(R.id.btn_wednesday);
        btnThursday = findViewById(R.id.btn_thursday);
        btnFriday = findViewById(R.id.btn_friday);
        btnSaturday = findViewById(R.id.btn_saturday);
        btnSunday = findViewById(R.id.btn_sunday);
        tvSunriseSunsetCondition = findViewById(R.id.tv_sunrise_sunset_condition);
        sunriseCheckbox = findViewById(R.id.sunrise_check);
        sunsetCheckbox = findViewById(R.id.sunset_check);
        setOnClickListener(settingReferenceArea);
        setOnClickListener(R.id.btn_monday, R.id.btn_tuesday, R.id.btn_wednesday, R.id.btn_thursday, R.id.btn_friday, R.id.btn_saturday, R.id.btn_sunday, R.id.btn_sunrise, R.id.btn_sunset);
        btnSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    selectDayLayout.setVisibility(View.VISIBLE);
                } else {
                    selectDayLayout.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    protected void initData() {
        serialNo = getString("serial_no");
        mLinkageBean = LinkageDataHolder.getLinkageData();
        if (mLinkageBean != null) {
            List<LinkageBean.LinkageCondition> linkageConditions = mLinkageBean.getLinkageConditions();
            if (linkageConditions != null && linkageConditions.size() > 0 && !TextUtils.isEmpty(serialNo)) {
                for (LinkageBean.LinkageCondition item : linkageConditions) {
                    if (item.getConditionSerialNo().equals(serialNo)) {
                        mLinkageCondition = item;
                        areaName = mLinkageCondition.getAreaName();
                        areaCoding = mLinkageCondition.getAreaCoding();
                        sunPositionType = mLinkageCondition.getSunPositionType();
                        timeLong = mLinkageCondition.getTimeLong();
                        latitude = mLinkageCondition.getLatitude();
                        longitude = mLinkageCondition.getLongitude();
                        scopeLong = mLinkageCondition.getScopeLong();
                        break;
                    }
                }
            }
        }
        updateView();
    }

    private void updateView() {
        mClickSunriseSunsetType = sunPositionType;
        if (TextUtils.isEmpty(areaName)) {
            longitude = AppApplication.getInstance().getCurHouseInfo().getLongitude();
            latitude = AppApplication.getInstance().getCurHouseInfo().getLatitude();
            scopeLong = AppApplication.getInstance().getCurHouseInfo().getRange();
            areaName = AppApplication.getInstance().getCurHouseInfo().getProvince() + " " + AppApplication.getInstance().getCurHouseInfo().getCity() + " " + AppApplication.getInstance().getCurHouseInfo().getDistrict();
            areaCoding = AppApplication.getInstance().getCurHouseInfo().getDistrictId();
        }
        settingReferenceArea.setRightText(areaName);
        if (mLinkageCondition != null) {
            btnSwitch.setChecked(mLinkageCondition.getTimingType() == 1);
            btnMonday.setSelected(mLinkageCondition.isMonday());
            btnTuesday.setSelected(mLinkageCondition.isTuesday());
            btnWednesday.setSelected(mLinkageCondition.isWednesday());
            btnThursday.setSelected(mLinkageCondition.isThursday());
            btnFriday.setSelected(mLinkageCondition.isFriday());
            btnSaturday.setSelected(mLinkageCondition.isSaturday());
            btnSunday.setSelected(mLinkageCondition.isSunday());
        }
        String text = null;
        switch (timeLong){
            case -120 * 60 * 1000:
                text = getResources().getString(R.string.when_2_hours_ago);
                break;
            case -60 * 60 * 1000:
                text = getResources().getString(R.string.when_1_hours_ago);
                break;
            case -30 * 60 * 1000:
                text = getResources().getString(R.string.when_30_minutes_ago);
                break;
            case -10 * 60 * 1000:
                text = getResources().getString(R.string.when_10_minutes_ago);
                break;
            case -5 * 60 * 1000:
                text = getResources().getString(R.string.when_5_minutes_ago);
                break;
            case 0:
                text = getResources().getString(R.string.when_time);
                break;
            case 5 * 60 * 1000:
                text = getResources().getString(R.string.when_5_minutes_later);
                break;
            case 10 * 60 * 1000:
                text = getResources().getString(R.string.when_10_minutes_later);
                break;
            case 30 * 60 * 1000:
                text = getResources().getString(R.string.when_30_minutes_later);
                break;
            case 60 * 60 * 1000:
                text = getResources().getString(R.string.when_1_hours_later);
                break;
            case 120 * 60 * 1000:
                text = getResources().getString(R.string.when_2_hours_later);
                break;
        }

        //1日出、2日落
        switch (sunPositionType) {
            case 1:
                sunriseCheckbox.setSelected(true);
                sunsetCheckbox.setSelected(false);
                text = getString(R.string.sunrise) + text;
                sunriseCheckbox.setText(text);
                sunsetCheckbox.setText(null);
                break;
            case 2:
                sunriseCheckbox.setSelected(false);
                sunsetCheckbox.setSelected(true);
                text = getString(R.string.sunset) + text;
                sunriseCheckbox.setText(null);
                sunsetCheckbox.setText(text);
                break;
        }
    }

    @Override
    public void onRightClick(View view) {
        if (TextUtils.isEmpty(areaName)) {
            toast(R.string.please_select_reference_area_hint);
            return;
        }
        if(btnSwitch.isChecked()
                && !btnMonday.isSelected()
                && !btnTuesday.isSelected()
                && !btnWednesday.isSelected()
                && !btnThursday.isSelected()
                && !btnFriday.isSelected()
                && !btnSaturday.isSelected()
                && !btnSunday.isSelected()){
            toast(R.string.please_select_repeat_day_hint);
            return;
        }
        if (sunPositionType == 0) {
            toast(R.string.please_select_sunrise_sunset_time);
            return;
        }
        //当前联动空，才去
        if (mLinkageCondition == null) {
            //本地保存联动条件
            if (mLinkageBean == null) {
                mLinkageBean = new LinkageBean();
                LinkageDataHolder.setLinkageData(mLinkageBean);
            }
            List<LinkageBean.LinkageCondition> linkageConditions = mLinkageBean.getLinkageConditions();
            if (linkageConditions == null) {
                linkageConditions = new ArrayList<>();
            }
            mLinkageBean.setLinkageConditions(linkageConditions);
            mLinkageCondition = new LinkageBean.LinkageCondition();
            //先设置个本地的SerialNo，为了编辑找到对应的数据
            mLinkageCondition.setConditionSerialNo(UUID.randomUUID().toString());
            mLinkageCondition.setConditionType(6);
            linkageConditions.add(mLinkageCondition);
            mLinkageCondition.setSort(linkageConditions.indexOf(mLinkageCondition) + 1);
        }
        mLinkageCondition.setAreaType(2);
        mLinkageCondition.setAreaName(areaName);
        mLinkageCondition.setAreaCoding(areaCoding);
        mLinkageCondition.setMonday(btnSwitch.isChecked() && btnMonday.isSelected());
        mLinkageCondition.setTuesday(btnSwitch.isChecked() && btnTuesday.isSelected());
        mLinkageCondition.setWednesday(btnSwitch.isChecked() && btnWednesday.isSelected());
        mLinkageCondition.setThursday(btnSwitch.isChecked() && btnThursday.isSelected());
        mLinkageCondition.setFriday(btnSwitch.isChecked() && btnFriday.isSelected());
        mLinkageCondition.setSaturday(btnSwitch.isChecked() && btnSaturday.isSelected());
        mLinkageCondition.setSunday(btnSwitch.isChecked() && btnSunday.isSelected());
        //0不循环、1循环
        mLinkageCondition.setTimingType(btnSwitch.isChecked() ? 1 : 0);
        mLinkageCondition.setSunPositionType(sunPositionType);
        mLinkageCondition.setTimeLong(timeLong);
        mLinkageCondition.setLongitude(longitude);
        mLinkageCondition.setLatitude(latitude);
        mLinkageCondition.setScopeLong(scopeLong);
        mLinkageCondition.setImageUrl(sunPositionType == 1 ? "icon_intelligent_environment_sunrise" : "icon_intelligent_environment_sunset");
        mLinkageCondition.setBriefing(getString(R.string.sunrise_sunset));
        startActivity_SingleTask(EditLinkageActivity.class);
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setting_reference_area:
//                showBottomDistrictDialog();
                Intent intent = new Intent(getContext(), LocationActivity.class);
                LocationData locationData = new LocationData();
                if (longitude > 0 && latitude > 0) {
                    locationData.location = longitude + ":" + latitude;
                    locationData.scopeLong = scopeLong;
                } else {
                    if (AppApplication.getInstance().getCurHouseInfo() != null) {
                        locationData.location = AppApplication.getInstance().getCurHouseInfo().getLongitude() + ":" + AppApplication.getInstance().getCurHouseInfo().getLatitude();
                    }
                }
                LocationActivity.startForResult(this, locationData, 1, LocationActivity.LOCATION_REQUEST_CODE);
                break;
            case R.id.btn_sunrise:
                mClickSunriseSunsetType = 1;
                showBottomSunriseSunsetDialog();
                break;
            case R.id.btn_sunset:
                mClickSunriseSunsetType = 2;
                showBottomSunriseSunsetDialog();
                break;
            case R.id.btn_monday:
            case R.id.btn_tuesday:
            case R.id.btn_wednesday:
            case R.id.btn_thursday:
            case R.id.btn_friday:
            case R.id.btn_saturday:
            case R.id.btn_sunday:
                view.setSelected(!view.isSelected());
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == LocationActivity.LOCATION_REQUEST_CODE) {
                LocationData locationData = (LocationData) data.getSerializableExtra("DATA");
                longitude = locationData.longitude;
                latitude = locationData.latitude;
                scopeLong = locationData.scopeLong;
                areaName = locationData.province + " " + locationData.city + " " + locationData.district;
                areaCoding = locationData.provinceId + locationData.cityId + locationData.districtId;
                settingReferenceArea.setRightText(areaName);
            }
        }
    }

//    private BottomDistrictDialog mBottomDistrictDialog;
//
//    private void showBottomDistrictDialog() {
//        if (mBottomDistrictDialog == null) {
//            mBottomDistrictDialog = new BottomDistrictDialog(this);
//            mBottomDistrictDialog.setDialogClickListener(new BottomDistrictDialog.DialogClickListener() {
//                @Override
//                public void onConfirmClick(String text, String code) {
//                    settingReferenceArea.setRightText(text);
//                    mBottomDistrictDialog.dismiss();
//                }
//            });
//            new XPopup.Builder(this)
//                    .dismissOnBackPressed(true)
//                    .enableDrag(true)
//                    .asCustom(mBottomDistrictDialog);
//        }
//        mBottomDistrictDialog.show();
//    }

    private void showBottomSunriseSunsetDialog() {
        if (mBottomSunriseSunsetDialog == null) {
            mBottomSunriseSunsetDialog = new BottomSunriseSunsetDialog(this);
            mBottomSunriseSunsetDialog.setDialogClickListener(new BottomSunriseSunsetDialog.DialogClickListener() {
                @Override
                public void onConfirmClick(SunriseSunsetTimeBean sunriseSunsetTimeBean) {
                    if (sunriseSunsetTimeBean != null) {
                        mBottomSunriseSunsetDialog.dismiss();
                        sunPositionType = mClickSunriseSunsetType;
                        String text = null;
                        switch (sunPositionType) {
                            case 1:
                                sunriseCheckbox.setSelected(true);
                                sunsetCheckbox.setSelected(false);
                                timeLong = sunriseSunsetTimeBean.getValue() * 1000;
                                text = getString(R.string.sunrise) + sunriseSunsetTimeBean.getLabel();
                                sunriseCheckbox.setText(text);
                                sunsetCheckbox.setText(null);
//                                tvSunriseSunsetCondition.setText(getString(R.string.effective_when_holder, text));
                                break;
                            case 2:
                                sunriseCheckbox.setSelected(false);
                                sunsetCheckbox.setSelected(true);
                                text = getString(R.string.sunset) + sunriseSunsetTimeBean.getLabel();
                                timeLong = sunriseSunsetTimeBean.getValue() * 1000;
                                sunriseCheckbox.setText(null);
                                sunsetCheckbox.setText(text);
//                                tvSunriseSunsetCondition.setText(getString(R.string.effective_when_holder, text));
                                break;
                        }
                    }
                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomSunriseSunsetDialog);
        }
        mBottomSunriseSunsetDialog.setSunriseSunsetType(mClickSunriseSunsetType);
        //当前选中和点击一样
        if(sunPositionType == mClickSunriseSunsetType){
            mBottomSunriseSunsetDialog.setTimeLong(timeLong / 1000);
        }else{
            mBottomSunriseSunsetDialog.setTimeLong(0);
        }
        mBottomSunriseSunsetDialog.show();
    }
}